package bb.lanxing.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import bb.lanxing.R;
import bb.lanxing.model.json.RankUserDetail;
import bb.lanxing.util.Log;
import bb.lanxing.util.MedalUtil;
import bb.lanxing.view.FontTextView;
import bb.lanxing.view.UserAvatarView;

public class SportsRankDetailAdapter extends LoadMoreAdapter {
    private final Context context;
    private final List<RankUserDetail> userList;

    public SportsRankDetailAdapter(List<RankUserDetail> userList, Context context) {
        this.userList = userList;
        this.context = context;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void bindUserDataToView(ViewHolder holder, RankUserDetail userDetail) {
        holder.userName.setText(userDetail.getUserName());
        holder.itemRankNum.setText(String.valueOf(userDetail.getRank()));
        holder.heat.setText(String.valueOf(userDetail.getHots()));
        holder.itemCityName.setText(userDetail.getCityName());
        setupTeamNameView(holder.itemTeamName, userDetail.getMainTeam());
    }

    private void setupTeamNameView(TextView teamNameView, String teamName) {
        if (TextUtils.isEmpty(teamName)) {
            teamNameView.setVisibility(View.GONE);
        } else {
            teamNameView.setVisibility(View.VISIBLE);
            String[] teamInfo = teamName.split(":");
            teamNameView.setText(teamInfo.length >= 2 ? teamInfo[0] : "");
        }
    }

    private void setupAvatarView(UserAvatarView avatarView, RankUserDetail userDetail) {
        avatarView.setAvatarMode(UserAvatarView.AVATAR_SIZE_48);
        avatarView.setAvatarForUrl(userDetail.getAvatar());
        avatarView.setUserLevelText(userDetail.getLevel());

        String proName = userDetail.getProName();
        boolean hasProName = !TextUtils.isEmpty(proName);
        avatarView.isShowAvatarFrame(hasProName);
        avatarView.setProTitle(hasProName ? proName : null);
    }

    private void setupMedalView(ViewHolder holder, RankUserDetail userDetail) {
        boolean hasProName = !TextUtils.isEmpty(userDetail.getProName());
        MedalUtil.showAvatarMedal(userDetail.getMedalSmall(), holder.medalContainerLayout,
                userDetail.getPlateNum(), hasProName, context, userDetail.getUserAvatarMedals(),
                userDetail.getLicenseNumberSkin(), userDetail.getLicenseNumberColor());
    }

    private void checkAndTriggerLoadMore(int position) {
        if (loadMoreEnabled && !loadingMore && position >= userList.size() - 2 && onLoadEndListener != null) {
            loadingMore = true;
            onLoadEndListener.onLoadEnd();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        Log.d("position = " + position + " size = " + userList.size() + " , " + loadingMore + ", " + loadMoreEnabled);
        checkAndTriggerLoadMore(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rank_list_detail_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (this.userList.size() <= 0) {
            return convertView;
        }
        RankUserDetail userDetail = this.userList.get(position);
        bindUserDataToView(viewHolder, userDetail);
        setupAvatarView(viewHolder.avatar, userDetail);
        setupMedalView(viewHolder, userDetail);
        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return this.userList.get(position);
    }

    @Override
    public int getCount() {
        return this.userList.size();
    }

    public class ViewHolder {
        private final TextView itemCityName;
        private final FontTextView heat;
        private final TextView itemTeamName;
        private final LinearLayout medalContainerLayout;
        private final TextView userName;
        private final UserAvatarView avatar;
        private final TextView itemRankNum;

        public ViewHolder(View v) {
            userName = v.findViewById(R.id.item_user_name);
            itemRankNum = v.findViewById(R.id.item_rank_num);
            heat = v.findViewById(R.id.heat);
            itemTeamName = v.findViewById(R.id.item_team_name);
            itemCityName = v.findViewById(R.id.item_city_name);
            avatar = v.findViewById(R.id.avatar);
            medalContainerLayout = v.findViewById(R.id.medal_container_layout);
        }
    }
}
